package com.qqt.csr.message.assembler;

import com.qqt.csr.message.entity.MessageNotify;
import com.qqt.csr.message.enums.NotifyType;
import com.qqt.csr.message.enums.ReadFlag;
import com.qqt.csr.message.event.Template;
import com.qqt.csr.message.event.intrasite.workorder.WorkOrderEventSource;

import java.util.Collections;
import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2024/12/18 9:01
 * @Description: 消息通知 装配器
 **/
public class MessageNotifyAssembler {

    /**
     * 构建站内信集合
     * @param source {@link WorkOrderEventSource} 工单事件源
     * @param currentUserId {@link Long} 当前用户id
     * @param notifyType {@link NotifyType} 通知类型
     * @return {@link List<MessageNotify>} 消息通知集合
     */
    public static List<MessageNotify> convert(WorkOrderEventSource source, Long currentUserId, NotifyType notifyType) {
        // 接收方
        if (source == null || source.getRecipientIds() == null || source.getRecipientIds().isEmpty()) {
            return Collections.emptyList();
        }
        // 模板
        Template template = source.getEventType();
        // 扩展信息
        String ext = source.getExt();
        // 链接地址
        String linkUrl = source.getLinkUrl();
        // 标题
        String title = String.format("工单<%s>", source.getWorkOrderName());
        // 第三方id
        String thirdId = String.valueOf(source.getWorkOrderId());
        // 内容
        String content = formatContent(template.getContentTemplate(), source.getParams());
        return source.getRecipientIds().stream()
                .map(recipientId -> convert(notifyType, currentUserId, template, ext, title, thirdId, linkUrl, content, recipientId.toString()))
                .toList();
    }

    /**
     * 构建站内信对象
     * @param notifyType {@link NotifyType} 通知类型
     * @param template {@link Template} 事件模板
     * @param ext {@link String} 扩展信息
     * @param title {@link String} 标题
     * @param thirdId {@link String} 关联id
     * @param linkUrl {@link String} 链接地址
     * @param content {@link String} 内容
     * @param recipientId {@link String} 接收者id
     * @return {@link MessageNotify} 站内信对象
     */
    public static MessageNotify convert(NotifyType notifyType, Long currentUserId, Template template, String ext, String title, String thirdId, String linkUrl, String content, String recipientId) {
        return MessageNotify.builder()
                .messageType(template.getMessageType().getValue())
                .actionType(template.getActionType().getValue())
                .ext(ext)
                .title(title)
                .content(content)
                .thirdId(thirdId)
                .readFlag(ReadFlag.UNREAD.getValue())
                .notifyType(notifyType.getValue())
                .linkUrl(linkUrl)
                .creatorId(currentUserId)
                .recipientId(recipientId)
                .build();
    }

    /**
     * 若参数不为空，则格式化内容并返回，否则返回模板
     * @param template {@link String} 模板
     * @param params {@link Object}[] 参数
     * @return {@link String} 内容
     */
    private static String formatContent(String template, Object[] params) {
        return params != null && params.length > 0 ? String.format(template, params) : template;
    }

}
